package property.service.impl;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import property.mapper.InfrastructureMapper;
import property.pojo.Infrastructure;
import property.service.InfrastructureService;
import property.utils.mapper.MyBatisUtil;

import java.util.regex.PatternSyntaxException;

public class InfrastructureServiceImpl implements InfrastructureService {

    @Override
    public int addInfrastructure(Infrastructure infrastructure) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        InfrastructureMapper mapper = sqlSession.getMapper(InfrastructureMapper.class);
        int i = mapper.insertSelective(infrastructure);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int deleteInfrastructure(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        InfrastructureMapper mapper = sqlSession.getMapper(InfrastructureMapper.class);
        int i;
        try {
            i= mapper.deleteByPrimaryKey(id);
        }catch (PersistenceException e){
            i = -1;
        }
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public int updateInfrastructure(Infrastructure infrastructure) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        InfrastructureMapper mapper = sqlSession.getMapper(InfrastructureMapper.class);
        int i;
        try {
            i = mapper.updateByPrimaryKeySelective(infrastructure);
        }catch (PersistenceException e){
            i = -1;
        }
        sqlSession.commit();
        sqlSession.close();
        return i;
    }


}
